#coding:utf-8
import datetime
def Moba(dbObject,data):
	## 查看报名战队赛点信息
	event_id = data.get("id")
	datas = dbObject.check_t_g_c(data.get("game_id"),event_id)
	_num = evolution2(len(datas))
	_false =0
	conn, cur = dbObject.mysqlcur()
	old_list =[]
	new_list = []
	for i in range(0,_num):
		if i==0:
			pid = 0
			insert_s,val = dbObject.insert_sql("event_match",dict(
							event_id = event_id,
							type = 1,
							create_time = datetime.datetime.now(),
							pid = 0
							))
			ret = cur.execute(insert_s%val)
			if ret==1:
				new_list.append(cur.lastrowid)
			else:
				_false = 1

		else:
			for _id in old_list:
				for _i in range(0,2):
					insert_s,val = dbObject.insert_sql("event_match",dict(
												event_id = event_id,
												type = 1,
												create_time = datetime.datetime.now(),
												pid  = _id
												))
					ret = cur.execute(insert_s%val)
					if ret==1:
						new_list.append(cur.lastrowid)
					else:
						_false =1

		if _false==1:
			conn.rollback()
			conn.close()
			cur.close()
			continue
		old_list = new_list
		new_list = []

	if _false==0:
		for i in range(0,len(old_list)):
			pid = old_list[i]
			if i==0:
				start_index = 0
				end_index = 2
			else:
				start_index = 2*i
				end_index = 2*i+2
			for data in datas[start_index:end_index]:
				ret =dbObject.add("event_match",dict(
										event_id = event_id,
										team_id = data.get("team_id"),
										type = 1,
										create_time = datetime.datetime.now(),
										pid = pid
					))
				if not ret:
					_false =1
			if _false==1:
				conn.rollback()
				conn.close()
				cur.close()
				continue
		if _false==0:
			dbObject.update("hot_event",dict(status = 1), dict(id =event_id ))
			conn.commit()
			conn.close()
			cur.close()

def evolution2(length):
	num = 1
	while length/2!=1:
		num+=1
		length = length/2
	return num
